function varargout = microgen_viability(varargin)
% MICROGEN_VIABILITY M-file for microgen_viability.fig
%      MICROGEN_VIABILITY, by itself, creates a new MICROGEN_VIABILITY or raises the existing
%      singleton*.
%
%      H = MICROGEN_VIABILITY returns the handle to a new MICROGEN_VIABILITY or the handle to
%      the existing singleton*.
%
%      MICROGEN_VIABILITY('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MICROGEN_VIABILITY.M with the given input arguments.
%
%      MICROGEN_VIABILITY('Property','Value',...) creates a new MICROGEN_VIABILITY or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before microgen_viability_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to microgen_viability_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Copyright 2002-2003 The MathWorks, Inc.

% Edit the above text to modify the response to help microgen_viability

% Last Modified by GUIDE v2.5 01-Jul-2008 22:16:11

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @microgen_viability_OpeningFcn, ...
                   'gui_OutputFcn',  @microgen_viability_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before microgen_viability is made visible.
function microgen_viability_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to microgen_viability (see VARARGIN)

% Choose default command line output for microgen_viability
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes microgen_viability wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = microgen_viability_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on selection change in wind_speed.
function wind_speed_Callback(hObject, eventdata, handles)
% hObject    handle to wind_speed (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns wind_speed contents as cell array
%        contents{get(hObject,'Value')} returns selected item from wind_speed


% --- Executes during object creation, after setting all properties.
function wind_speed_CreateFcn(hObject, eventdata, handles)
% hObject    handle to wind_speed (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
set(hObject, 'String', {'Bellmullet', 'Kilkenny', 'Dublin Airport','User file'});


function user_turbulence_Callback(hObject, eventdata, handles)
% hObject    handle to user_turbulence (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of user_turbulence as text
%        str2double(get(hObject,'String')) returns contents of user_turbulence as a double


% --- Executes during object creation, after setting all properties.
function user_turbulence_CreateFcn(hObject, eventdata, handles)
% hObject    handle to user_turbulence (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in turbulence.
function turbulence_Callback(hObject, eventdata, handles)
% hObject    handle to turbulence (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns turbulence contents as cell array
%        contents{get(hObject,'Value')} returns selected item from turbulence


% --- Executes during object creation, after setting all properties.
function turbulence_CreateFcn(hObject, eventdata, handles)
% hObject    handle to turbulence (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
set(hObject, 'String', {'Large flat plain' 'Open rural area', 'Urban area', 'User file'});

% --- Executes on selection change in turbine.
function turbine_Callback(hObject, eventdata, handles)
% hObject    handle to turbine (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns turbine contents as cell array
%        contents{get(hObject,'Value')} returns selected item from turbine


% --- Executes during object creation, after setting all properties.
function turbine_CreateFcn(hObject, eventdata, handles)
% hObject    handle to turbine (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
set(hObject, 'String', {'Stealthgen 400W', 'Proven 600W','B&Q 1.2kW turbine','Swift 1.5kW', 'Proven 2.5kW', 'User file'});



function user_turbine_Callback(hObject, eventdata, handles)
% hObject    handle to user_turbine (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of user_turbine as text
%        str2double(get(hObject,'String')) returns contents of user_turbine as a double


% --- Executes during object creation, after setting all properties.
function user_turbine_CreateFcn(hObject, eventdata, handles)
% hObject    handle to user_turbine (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function space_in_wind_Callback(hObject, eventdata, handles)
% hObject    handle to space_in_wind (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of space_in_wind as text
%        str2double(get(hObject,'String')) returns contents of space_in_wind as a double


% --- Executes during object creation, after setting all properties.
function space_in_wind_CreateFcn(hObject, eventdata, handles)
% hObject    handle to space_in_wind (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end







function space_cross_wind_Callback(hObject, eventdata, handles)
% hObject    handle to space_cross_wind (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of space_cross_wind as text
%        str2double(get(hObject,'String')) returns contents of space_cross_wind as a double


% --- Executes during object creation, after setting all properties.
function space_cross_wind_CreateFcn(hObject, eventdata, handles)
% hObject    handle to space_cross_wind (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end









function override_turbines_Callback(hObject, eventdata, handles)
% hObject    handle to override_turbines (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of override_turbines as text
%        str2double(get(hObject,'String')) returns contents of override_turbines as a double


% --- Executes during object creation, after setting all properties.
function override_turbines_CreateFcn(hObject, eventdata, handles)
% hObject    handle to override_turbines (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end




% --- Executes on selection change in radiation.
function radiation_Callback(hObject, eventdata, handles)
% hObject    handle to radiation (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns radiation contents as cell array
%        contents{get(hObject,'Value')} returns selected item from radiation


% --- Executes during object creation, after setting all properties.
function radiation_CreateFcn(hObject, eventdata, handles)
% hObject    handle to radiation (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
set(hObject, 'String', {'Belmullet','Kilkenny','Dublin Airport','User file'});

% --- Executes on selection change in power_curve_PV.
function power_curve_PV_Callback(hObject, eventdata, handles)
% hObject    handle to power_curve_PV (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns power_curve_PV contents as cell array
%        contents{get(hObject,'Value')} returns selected item from power_curve_PV


% --- Executes during object creation, after setting all properties.
function power_curve_PV_CreateFcn(hObject, eventdata, handles)
% hObject    handle to power_curve_PV (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

set(hObject, 'String', {'Shell ST 40 (40W)','Photowatt PW750 (85W)','Kyocera KC125G (125W)','Sharp NE-165U1 (165W)','Sharp NT-180U1 (180W)','User file'});

% --- Executes on selection change in time_solar_noon.
function time_solar_noon_Callback(hObject, eventdata, handles)
% hObject    handle to time_solar_noon (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns time_solar_noon contents as cell array
%        contents{get(hObject,'Value')} returns selected item from time_solar_noon


% --- Executes during object creation, after setting all properties.
function time_solar_noon_CreateFcn(hObject, eventdata, handles)
% hObject    handle to time_solar_noon (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
set(hObject, 'String', {'Use built in data', 'User file'});






% --- Executes on selection change in latitude.
function latitude_Callback(hObject, eventdata, handles)
% hObject    handle to latitude (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns latitude contents as cell array
%        contents{get(hObject,'Value')} returns selected item from latitude


% --- Executes during object creation, after setting all properties.
function latitude_CreateFcn(hObject, eventdata, handles)
% hObject    handle to latitude (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
set(hObject, 'String', {'Use built in data', 'User latitude'});


function user_latitude_Callback(hObject, eventdata, handles)
% hObject    handle to user_latitude (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of user_latitude as text
%        str2double(get(hObject,'String')) returns contents of user_latitude as a double


% --- Executes during object creation, after setting all properties.
function user_latitude_CreateFcn(hObject, eventdata, handles)
% hObject    handle to user_latitude (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function azimuth_Callback(hObject, eventdata, handles)
% hObject    handle to azimuth (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of azimuth as text
%        str2double(get(hObject,'String')) returns contents of azimuth as a double


% --- Executes during object creation, after setting all properties.
function azimuth_CreateFcn(hObject, eventdata, handles)
% hObject    handle to azimuth (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function module_slope_Callback(hObject, eventdata, handles)
% hObject    handle to module_slope (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of module_slope as text
%        str2double(get(hObject,'String')) returns contents of module_slope as a double


% --- Executes during object creation, after setting all properties.
function module_slope_CreateFcn(hObject, eventdata, handles)
% hObject    handle to module_slope (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function area_length_Callback(hObject, eventdata, handles)
% hObject    handle to area_length (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of area_length as text
%        str2double(get(hObject,'String')) returns contents of area_length as a double


% --- Executes during object creation, after setting all properties.
function area_length_CreateFcn(hObject, eventdata, handles)
% hObject    handle to area_length (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function area_width_Callback(hObject, eventdata, handles)
% hObject    handle to area_width (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of area_width as text
%        str2double(get(hObject,'String')) returns contents of area_width as a double


% --- Executes during object creation, after setting all properties.
function area_width_CreateFcn(hObject, eventdata, handles)
% hObject    handle to area_width (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in module_dimensions.
function module_dimensions_Callback(hObject, eventdata, handles)
% hObject    handle to module_dimensions (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns module_dimensions contents as cell array
%        contents{get(hObject,'Value')} returns selected item from module_dimensions


% --- Executes during object creation, after setting all properties.
function module_dimensions_CreateFcn(hObject, eventdata, handles)
% hObject    handle to module_dimensions (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
set(hObject, 'String', {'Use built in data', 'User dimensions'});

function user_module_length_Callback(hObject, eventdata, handles)
% hObject    handle to user_module_length (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of user_module_length as text
%        str2double(get(hObject,'String')) returns contents of user_module_length as a double


% --- Executes during object creation, after setting all properties.
function user_module_length_CreateFcn(hObject, eventdata, handles)
% hObject    handle to user_module_length (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit16_Callback(hObject, eventdata, handles)
% hObject    handle to edit16 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit16 as text
%        str2double(get(hObject,'String')) returns contents of edit16 as a double


% --- Executes during object creation, after setting all properties.
function edit16_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit16 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function user_module_width_Callback(hObject, eventdata, handles)
% hObject    handle to user_module_width (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of user_module_width as text
%        str2double(get(hObject,'String')) returns contents of user_module_width as a double


% --- Executes during object creation, after setting all properties.
function user_module_width_CreateFcn(hObject, eventdata, handles)
% hObject    handle to user_module_width (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in temperature.
function temperature_Callback(hObject, eventdata, handles)
% hObject    handle to temperature (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns temperature contents as cell array
%        contents{get(hObject,'Value')} returns selected item from temperature


% --- Executes during object creation, after setting all properties.
function temperature_CreateFcn(hObject, eventdata, handles)
% hObject    handle to temperature (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
set(hObject, 'String', {'Use built in data', 'User file'});


function power_electronics_Callback(hObject, eventdata, handles)
% hObject    handle to power_electronics (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of power_electronics as text
%        str2double(get(hObject,'String')) returns contents of power_electronics as a double


% --- Executes during object creation, after setting all properties.
function power_electronics_CreateFcn(hObject, eventdata, handles)
% hObject    handle to power_electronics (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function period_loan_Callback(hObject, eventdata, handles)
% hObject    handle to period_loan (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of period_loan as text
%        str2double(get(hObject,'String')) returns contents of period_loan as a double


% --- Executes during object creation, after setting all properties.
function period_loan_CreateFcn(hObject, eventdata, handles)
% hObject    handle to period_loan (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function cost_kwh_Callback(hObject, eventdata, handles)
% hObject    handle to cost_kwh (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of cost_kwh as text
%        str2double(get(hObject,'String')) returns contents of cost_kwh as a double


% --- Executes during object creation, after setting all properties.
function cost_kwh_CreateFcn(hObject, eventdata, handles)
% hObject    handle to cost_kwh (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function APR_Callback(hObject, eventdata, handles)
% hObject    handle to APR (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of APR as text
%        str2double(get(hObject,'String')) returns contents of APR as a double


% --- Executes during object creation, after setting all properties.
function APR_CreateFcn(hObject, eventdata, handles)
% hObject    handle to APR (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function bat_efficiency_Callback(hObject, eventdata, handles)
% hObject    handle to bat_efficiency (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of bat_efficiency as text
%        str2double(get(hObject,'String')) returns contents of bat_efficiency as a double


% --- Executes during object creation, after setting all properties.
function bat_efficiency_CreateFcn(hObject, eventdata, handles)
% hObject    handle to bat_efficiency (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function bat_capacity_Callback(hObject, eventdata, handles)
% hObject    handle to bat_capacity (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of bat_capacity as text
%        str2double(get(hObject,'String')) returns contents of bat_capacity as a double


% --- Executes during object creation, after setting all properties.
function bat_capacity_CreateFcn(hObject, eventdata, handles)
% hObject    handle to bat_capacity (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function user_system_cost_Callback(hObject, eventdata, handles)
% hObject    handle to user_system_cost (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of user_system_cost as text
%        str2double(get(hObject,'String')) returns contents of user_system_cost as a double


% --- Executes during object creation, after setting all properties.
function user_system_cost_CreateFcn(hObject, eventdata, handles)
% hObject    handle to user_system_cost (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in system_cost.
function system_cost_Callback(hObject, eventdata, handles)
% hObject    handle to system_cost (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns system_cost contents as cell array
%        contents{get(hObject,'Value')} returns selected item from system_cost


% --- Executes during object creation, after setting all properties.
function system_cost_CreateFcn(hObject, eventdata, handles)
% hObject    handle to system_cost (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
set(hObject, 'String', {'Use built in data','User cost of system'});



% --- Executes on selection change in load_curve.
function load_curve_Callback(hObject, eventdata, handles)
% hObject    handle to load_curve (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns load_curve contents as cell array
%        contents{get(hObject,'Value')} returns selected item from load_curve


% --- Executes during object creation, after setting all properties.
function load_curve_CreateFcn(hObject, eventdata, handles)
% hObject    handle to load_curve (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
set(hObject, 'String', {'Average domestic load curve', 'Holiday home load curve','User load curve'});




% --- Executes on selection change in graph_select_1.
function graph_select_1_Callback(hObject, eventdata, handles)
% hObject    handle to graph_select_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns graph_select_1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from graph_select_1


% --- Executes during object creation, after setting all properties.
function graph_select_1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to graph_select_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
set(hObject, 'String', {'Pay back time (Time (years), money saved (Euro))' 'Sum money saved monthly (Time (months), Euro)', 'Discounted monthly repayments', 'User file'});


% --- Executes on selection change in graph_select_2.
function graph_select_2_Callback(hObject, eventdata, handles)
% hObject    handle to graph_select_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns graph_select_2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from graph_select_2


% --- Executes during object creation, after setting all properties.
function graph_select_2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to graph_select_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
set(hObject, 'String', {'Energy output of system (Time (hours), kWh)','Energy output of wind turbine(s) (Time (hours), kWh)' 'Energy output of solar panels (Time (hours), kWh)', 'Energy stored in batteries (Time (hours), kWh)', 'Solar radiation (Time (hours),W/m^2)','Wind speed (Time (hours), meters/s)'});




function height_Callback(hObject, eventdata, handles)
% hObject    handle to height (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of height as text
%        str2double(get(hObject,'String')) returns contents of height as a double


% --- Executes during object creation, after setting all properties.
function height_CreateFcn(hObject, eventdata, handles)
% hObject    handle to height (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



% --- Executes on button press in Calculate.
function Calculate_Callback(hObject, eventdata, handles)
% hObject    handle to Calculate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
num_hours = 26304;

guidata(hObject, handles);
%This function determines the wind data to be used in the calculation
popup_sel_index = get(handles.wind_speed, 'Value');

switch popup_sel_index
    case 1
        wind_speed = textread('C:\bel_wind.txt');
    case 2
        wind_speed = textread('C:\kil_wind.txt');
    case 3
        wind_speed = textread('C:\dub_air_wind.txt');
    case 4
        wind_speed = textread('C:\user_wind.txt');
end   
         

%This defines the turbulence to be used in the calculation
 popup_sel_index_turbulence = get(handles.turbulence, 'Value');
 switch popup_sel_index_turbulence
     case 1
         percent_turbulence = 0;
         roughness = .01;
     case 2
         %formuls and numbers for turbulence from 'climate considerations in building
         %and urban design' by Baruch Givoni
        percent_turbulence = 10
        roughness = .3;
     case 3
        percent_turbulence = 30
        roughness = .7;
     case 4
         percent_turbulence = round(textread('C:\user_percent_turbulence.txt'));
         roughness = .01;
 end  
 
%this defines the wind power curve to be used in the calculation 
guidata(hObject, handles);
popup_sel_index_turbine = get(handles.turbine, 'Value');
 switch popup_sel_index_turbine
     case 1
        power_curve_wind = textread('C:\stealthgen400w.txt');
        cost_turbine = 2085;
        rotor_diameter = 1.5;
     case 2
        power_curve_wind = textread('C:\proven600w.txt');
        rotor_diameter = 2.55;
        cost_turbine = 4497.5;
     case 3
        power_curve_wind = textread('C:\BQ_turbine.txt');
        rotor_diameter = 1.75;
        %better generation.co.uk
        cost_turbine = 2363;
     case 4
        power_curve_wind = textread('C:\swift1_5kw.txt'); 
        rotor_diameter = 2;
        cost_turbine = 5782.5;
     case 5
        power_curve_wind = textread('C:\proven2_5kw.txt');
        rotor_diameter = 3.5;
        cost_turbine = 10006.5;
     case 6
        power_curve_wind = textread('C:\user_power_curve_wind.txt'); 
        rotor_diameter = str2double(get(handles.user_rotor_diameter,'string'));
     
 
 end  


%This defines the remain variables needed to perform the calculation
space_in_wind = str2double(get(handles.space_in_wind,'string'));
space_cross_wind = str2double(get(handles.space_cross_wind,'string'));
user_rotor_diameter = str2double(get(handles.user_rotor_diameter,'string'));
override_num_turbines = str2double(get(handles.override_turbines,'string'));




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%SOLAR%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


guidata(hObject, handles);
popup_sel_index_radiation = get(handles.radiation, 'Value');
 switch popup_sel_index_radiation
     case 1
        radiation = textread('C:\bel_solar.txt');
        time_solar_noon = textread('C:\belmullet_solar_noon.txt');
        temperature = textread('C:\bel_temp.txt');
        latitude = 54.21;
        
     case 2
        radiation = textread('C:\kil_solar.txt');
        time_solar_noon = textread('C:\kil_solar_noon.txt');
        temperature = textread('C:\kil_temp.txt');
        latitude = 53.45;
        
     case 3
         radiation = textread('C:\dublin_airport_solar.txt');
         time_solar_noon = textread('C:\dub_air_solar_noon.txt');
         temperature = textread('C:\dub_air_temp.txt');
         latitude = 52.645;
         
     case 4
         radiation = textread('C:\user_solar_radiation.txt');
       
 end  


guidata(hObject, handles);
popup_sel_index_power_curve_PV = get(handles.power_curve_PV, 'Value');
 switch popup_sel_index_power_curve_PV
     case 1
        power_curve_PV = textread('C:\shell40_mono.txt');
        cost_module = 288;
        
     case 2
         power_curve_PV = textread('C:\photowatt85.txt');
         cost_module = 319.6;
     case 3
         power_curve_PV = textread('C:\kyocera125.txt');
         cost_module = 377;
         
     case 4
         power_curve_PV = textread('C:\sharp165.txt');
         cost_module = 509;
         
     case 5
         power_curve_PV = textread('C:\sharp180_mono.txt');
         cost_module = 618;
         
     case 6
        power_curve_PV = textread('C:\user_PV_power_curve.txt');
 end  




guidata(hObject, handles);
popup_sel_index_time_solar_noon = get(handles.time_solar_noon, 'Value');
 switch popup_sel_index_time_solar_noon
     case 1
        time_solar_noon = time_solar_noon;
     case 2
        time_solar_noon = textread('C:\user_time_solar_noon.txt');
 end  




guidata(hObject, handles);
popup_sel_index_temperature = get(handles.temperature, 'Value');
 switch popup_sel_index_temperature
     case 1
        temperature = temperature;
     case 2
        temperature = textread('C:\user_temperature.txt');
 end  



guidata(hObject, handles);
popup_sel_index_latitude = get(handles.latitude, 'Value');
 switch popup_sel_index_latitude
     case 1
        module_latitude = latitude;
     case 2
        module_latitude = str2double(get(handles.user_latitude,'String'));
 end  

%Data from remaining 'edit text' boxes
module_azimuth = str2double(get(handles.azimuth, 'String'));
module_slope = str2double(get(handles.module_slope, 'string'));
area_length = [str2num(get(handles.area_length, 'String'))];
area_width = [str2num(get(handles.area_width, 'String'))];



guidata(hObject, handles);
popup_sel_index_module_dimensions = get(handles.module_dimensions, 'Value');
 switch popup_sel_index_module_dimensions
     case 1
        module_width =  .652;
        module_length = 1.425;
     case 2
        module_width =  .556;
        module_length = 1.237;
     case 2
        module_width = str2double(get(handles.user_module_width,'String'));
        module_length = str2double(get(handles.user_module_length,'String'));
 end  


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Economics  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if get(handles.loan_yes,'Value')
    period_loan = str2double(get(handles.period_loan,'String'));
    APR = str2double(get(handles.APR,'String'));
else
    period_loan = 0;
    APR = 0;
end

cost_kwh = str2double(get(handles.cost_kwh,'String'));





guidata(hObject, handles);
popup_sel_index_load_curve = get(handles.load_curve, 'Value');
 switch popup_sel_index_load_curve
     case 1
        load_curve = textread('C:\domestic_load_curve.txt');
     case 2
        load_curve = textread('C:\holiday_load_curve.txt');
     case 3
         load_curve = textread('C:\user_load_curve.txt');
 end  

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This sections takes in battery capacity from the user %%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if  get(handles.storage_yes,'Value')
    bat_efficiency = str2double(get(handles.bat_efficiency,'String'));
    bat_capacity = str2double(get(handles.bat_capacity,'String'))   
else
    bat_efficiency = 0;
    bat_capacity = 0;
end

%function[energy_produced_wind]  = microgen_model(wind_speed, percent_turbulence, radiation, load_curve,power_curve_wind, power_curve_PV, time_solar_noon, temperature, module_latitude, module_azimuth, module_slope,area_length, area_width, module_length, module_width,  space_in_wind, space_cross_wind, rotor_diameter, override_num_turbines, power_electronics, bat_efficiency, bat_capacity, cost_kwh, period_loan, system_cost, APR, num_hours)
%This function allows the amount of power availible from a particular site
%to be calculted given the following inputs
%Wind_speed: wind speed in meters per second
%Power_curve: the power output of the turbine given a wind speed
%Num_hours: the number of hours being considered
%Space_in_wind: the space in meters availible facing the prevailing wind
%Space_cross_wind: the space availible at a right angle to the wind
%Rotor_diameter: the diameter of the turbines rotor in meters
%Percentage turbulence: the percentage by which the effective wind speed is
%reduced by turbulence
%Control_power: the amount of energy per hour the systems power electronics
%consume

%John Kelleher 10/02/2008 FYP project NUIM Maynooth


power_produced = 0;

if get(handles.wind_yes,'Value')
num_turbines_in_wind = (space_in_wind/(rotor_diameter*10));
num_turbines_cross_wind = (space_cross_wind/(rotor_diameter*3));

%Fix rounds these floats down to the nearest integer
num_turbines_in_wind = fix(num_turbines_in_wind);
num_turbines_cross_wind = fix(num_turbines_cross_wind);

num_turbines = (num_turbines_in_wind*num_turbines_cross_wind);

if num_turbines < override_num_turbines
    num_turbines = override_num_turbines;
end

num_turbines

for i = 1:num_hours
    if wind_speed(i) < 1
        power_produced(i) = 0;
    else
        
    height_velocity =  ((((str2double(get(handles.height,'String'))/10)^roughness)));   
    energy_produced_wind(i) = power_curve_wind(round((wind_speed(i)*(height_velocity)*(100 - percent_turbulence))));
    energy_produced_wind(i) = ((energy_produced_wind(i))*num_turbines)/1000;
    end
end
else
    energy_produced_wind(1:num_hours) = 0;
    height_velocity =  ((((str2double(get(handles.height,'String'))/10)^roughness)));
    num_turbines = 0
end



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This section calculates the max number of modules given dimensions%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if get(handles.solar_yes,'Value')
number_modules = 0;
for i = 1:length(area_length);
    l = area_length(i)/module_length;
    l = fix(l);
    m = area_width(i)/module_width;
    m = fix(m);
    lm = l*m;

    t = area_length(i)/module_width;
    t = fix(t);
    s = area_width(i)/module_length;
    s = fix(s);
    ts = t*s;

    if lm >= ts
     number_modules = number_modules + fix((lm/(module_length*module_width)));
    end
    if lm < ts
     number_modules = number_modules + fix((ts/(module_length*module_width)));
    end
end

num_modules = number_modules

    
power_electronics = str2double(get(handles.power_electronics,'String'));

guidata(hObject, handles);
popup_sel_index_system_cost = get(handles.system_cost, 'Value');
 switch popup_sel_index_system_cost
     case 1
        %From proven energy's price list
        bat_cost_kwh = 91.66;
        system_cost = (cost_module*num_modules) + (cost_turbine*num_turbines) + (bat_capacity*bat_cost_kwh)
     case 2
        system_cost = str2double(get(handles.user_system_cost,'String'));
 end  



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This section finds the hour angle%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
z = 1;

for i = 1:(num_hours/24)
    for j = 1:24;   

        simulation_time = (j/24);

        %using i + 8 as the simulation starts and ends on the 8th of january 
        diff = simulation_time - time_solar_noon(i);
        
        %algorithm from solarnoon.com
        hours = (diff*24)*15;
        minutes = ((diff - (fix(diff)))*(60/100));
        seconds = ((diff - (fix(minutes)))*(60/100));
        
        hour_angle(z) = hours + minutes + seconds;
  
        z = z+1;
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This calculates the angle between the solar beam and collector (module)%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%The kelly cosine table is taken from 'Wind and Solar Power Systems, Design
%Analysis and Operation , Second Edition ,Mukund R. Patel, P172
kelly_cosine = pchip([1:10],[ 1 .9789 .9299 .866 .7687 .634 .4499 .2783 .09674 -0.0979 ],[1:.0001:10]);

%this eliminates any negitive vlues produced by the interpolation
for i = 1:length(kelly_cosine)
    if kelly_cosine(i) < 0
        kelly_cosine(i) = 0;
    end
end


for i = 1:num_hours 
    
   
    %algorithm form Renewable Energy Resoources,John Twidell and Tony Weir
    %2006 Second Edition,p.95
    A = (sind(module_latitude))*(cosd(module_slope));
    B = (cosd(module_latitude))*(sind(module_slope))*(cosd(module_azimuth));
    C = (sind(module_slope))*(sind(module_azimuth));
    D = (cosd(module_latitude))*(cosd(module_slope));
    E = (sind(module_latitude))*(sind(module_slope))*(cosd(module_azimuth));

    solar_declination = (23.45)*sind((360*(284+((i)/24)))/365);

    cos_angle(i) = (A - B)*(sind(solar_declination)) + (C)*(sind(hour_angle(i))) + ((D + E)*cosd(hour_angle(i)))*(cosd(solar_declination));
    if cos_angle(i) < 0 
        cos_angle(i) = 0;
    end
    
    %this is multiplyed by the cell output current for a solar beam angle
    %of 90 degrees to get the output current for the cell at the present
    %angle. The angle is multiplied by 1000 to account for the interpolation used
    %and to reduce rounding error due to interger indexes
    temp = round(acosd((cos_angle(i)))*1000);
    
    I_angle(i) = kelly_cosine(temp);
      
end 


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This multiplys the module power curve with the availible radiation%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1:num_hours
    if radiation(i) < 1 
        energy_produced_PV(i) = 0;
    else
    energy_produced_PV(i) = (power_curve_PV(radiation(i))*I_angle(i));
    %power produced(i) to the left of the equals sign is divided by 1000
    %to get the power produced in Kwh
    energy_produced_PV(i) = (((energy_produced_PV(i)*number_modules)))/1000;
    
    %this accounts for non-ideal temperature, uses formula from, Photo
    %voltaic module thermal/wind performance: Long - term monitoring model
    %development for energy rating, Arizona state university east,
    %photovoltaic testing laboratory. 25 is the stantard testing
    %temperature for PV panels. assuming 30% wind turbulence
    
    module_temp(i) = (.943*temperature(i)) + (.028*radiation(i)) - ((1.528*round((wind_speed(i)*(height_velocity))))* ((100 - percent_turbulence)/100)) + 4.3;
    
    energy_produced_PV(i) = energy_produced_PV(i)*(1 - (0.005*abs(25 - module_temp(i))));
    end
end
else
    energy_produced_PV(1:num_hours) = 0;
    cost_module = 0;
    num_modules = 0;
    power_electronics = str2double(get(handles.power_electronics,'String'));
    %bat_efficiency = str2double(get(handles.bat_efficiency,'String'));
    %bat_capacity = str2double(get(handles.bat_capacity,'String'))
    guidata(hObject, handles);
    popup_sel_index_system_cost = get(handles.system_cost, 'Value');
    switch popup_sel_index_system_cost
        case 1
         %From proven energy's price list
         bat_cost_kwh = 91.66;
         system_cost = (cost_module*num_modules) + (cost_turbine*num_turbines) + (bat_capacity*bat_cost_kwh)
         case 2
         system_cost = str2double(get(handles.user_system_cost,'String'));
    end  
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This function calculates the reduction in electricity bills a wind turbine
%and solar PV panel can cause
buy_back_price = str2double(get(handles.buy_back_price,'String'))

if buy_back_price == 0;

    bat_energy(1:num_hours+1) = 0;

    energy_saved = 0;


     for i = 1:num_hours
    
     if(load_curve(i) - ((energy_produced_PV(i) + energy_produced_wind(i)))) > 0 && i == 1
         bat_energy(i) = 0;
     end
    
    
     total_power(i) = ((energy_produced_PV(i) + energy_produced_wind(i))*((power_electronics/100))) + (bat_energy(i)*(bat_efficiency/100));
    
     if (load_curve(i) - total_power(i)) > 0
         energy_saved = total_power(i);
            bat_energy(i+1) = 0;
     else
            energy_saved = load_curve(i);
     end
    
    
     if(load_curve(i) - ((energy_produced_PV(i) + energy_produced_wind(i)))) < 0 && bat_energy(i) + abs(load_curve(i) - ((energy_produced_PV(i) + energy_produced_wind(i)))) < bat_capacity
        bat_energy(i+1) = bat_energy(i) + abs(load_curve(i) - ((energy_produced_PV(i) + energy_produced_wind(i))));
     end
    
     
     if(load_curve(i) - ((energy_produced_PV(i) + energy_produced_wind(i)))) < 0 && bat_energy(i) + abs(load_curve(i) - ((energy_produced_PV(i) + energy_produced_wind(i)))) > bat_capacity
        bat_energy(i+1) = bat_capacity;
     end
    
   
     money_saved(i) = energy_saved*cost_kwh;
     acc_money_saved(i) = sum(money_saved(1:i));
     end
end  
    
 if buy_back_price ~= 0
     
      energy_saved = 0;
      buy_back_energy(i:num_hours) = 0;
      bat_energy(i:num_hours) = 0;

     for i = 1:num_hours
    
     total_power(i) = ((energy_produced_PV(i) + energy_produced_wind(i))*((power_electronics/100)));
    
     if load_curve(i) > total_power(i)
        energy_saved = total_power(i);
        buy_back_energy(i) = 0;
     else
       energy_saved = load_curve(i);
        buy_back_energy(i) = abs(load_curve(i) - total_power(i));
     end
    
     money_saved(i) = energy_saved*cost_kwh + (buy_back_energy(i)*buy_back_price);
     acc_money_saved(i) = sum(money_saved(1:i));
    
     end
 end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
grant_percentage = str2double(get(handles.grant,'String'))
system_cost = system_cost - (system_cost*(grant_percentage/100))

APR = APR/100;

%If no loan is taken out
if period_loan <= 0 || APR <= 0
    payback_period =     system_cost/((sum(money_saved))/(fix(num_hours/(8760))))
    %this allows the payback point to be displayed on the plot
    monthly_repay = system_cost/12;
    period_loan = 1;
%If a loan is taken out
else
    monthly_repay = system_cost*((APR/12)*((1 + ((APR)/12))^(12*period_loan)))/(((1 + ((APR)/12))^(12*period_loan)) -1)
    payback_period = (monthly_repay*12*period_loan)/((sum(money_saved))/(fix(num_hours/(8760))))
end
%This breaks the period into 36 pieces to represent 36 months



if get(handles.wind_yes,'Value') || get(handles.solar_yes,'Value') == 1

guidata(hObject, handles);
popup_sel_index_graph_select_1 = get(handles.graph_select_1, 'Value');
 switch popup_sel_index_graph_select_1
     case 1
         axes(handles.axes1)
         
         for i = 0:2
             yearly_money_saved(i+1) = acc_money_saved((1+i*8768));
         end
         
         %This extrapolates the line to produce a graph
         for i = 4:(fix(payback_period))*2
             yearly_money_saved(i) = yearly_money_saved(i-1) + (yearly_money_saved(i-2) - yearly_money_saved(i-3));
         end
        %This line indicates the pay back time
         plot([1 (payback_period*2)],[monthly_repay*period_loan*12 monthly_repay*period_loan*12],'g')
         
         hold on;
         plot(yearly_money_saved)
         legend('Payback line',' Sum of money saved')
         hold off;
         grid on;
        
     case 2
         
        for i = 0:35 
        monthly_money_saved(i+1) = acc_money_saved((1+i*fix(730.66666)));
        end
        
        axes(handles.axes1);
        plot(monthly_money_saved);
        
        grid on;
        
        
     case 3
        if period_loan <= 0 || APR <= 0
            monthly_repay = 0;
        end
        for i = 1:36
        money_per_month = sum(money_saved(1 +((i-1)*730):1:(730*i)));
        discounted_repay(i) = monthly_repay - money_per_month;
        end
        axes(handles.axes1);
        plot([0 36],[monthly_repay monthly_repay],'r')
        hold on
        plot(discounted_repay);
        legend('Monthly repayment','Discounted monthly repayment')
        grid on;
        hold off;
        save C:\vars\discounted_repay.txt discounted_repay -ascii
       
     case 4
       plot(acc_money_saved)
        axes(handles.axes1)
     case 5
        plot(acc_money_saved)
        axes(handles.axes1)
 end  
 
else
        yearly_money_saved = 0;
        monthly_money_saved = 0;
        discounted_repay = 0;
end

guidata(hObject, handles);
popup_sel_index_graph_select_2 = get(handles.graph_select_2, 'Value');
 switch popup_sel_index_graph_select_2
     case 1
         axes(handles.axes2);
         plot(energy_produced_wind + energy_produced_PV)

     case 2
         axes(handles.axes2);
         plot(energy_produced_wind)

     case 3
        axes(handles.axes2);
         plot(energy_produced_PV)
     case 4
        axes(handles.axes2);
        plot(bat_energy)
        
     case 5
        axes(handles.axes2);
        plot(radiation)
        
     case 6
        axes(handles.axes2);
        plot(wind_speed)
 end  
 
  save C:\vars\acc_money_saved.txt acc_money_saved -ascii
  save C:\vars\payback_period.txt payback_period -ascii
  save C:\vars\period_loan.txt period_loan -ascii
  save C:\vars\monthly_repay.txt monthly_repay -ascii
  save C:\vars\money_saved.txt money_saved -ascii
  save C:\vars\APR.txt APR -ascii
  save C:\vars\energy_produced_wind.txt energy_produced_wind -ascii
  save C:\vars\energy_produced_PV.txt energy_produced_PV -ascii
  save C:\vars\bat_energy.txt bat_energy -ascii
  save C:\vars\radiation.txt radiation -ascii
  save C:\vars\wind_speed.txt wind_speed -ascii
  save C:\vars\system_cost.txt system_cost -ascii
  if buy_back_price ~= 0
    save C:\vars\buy_back_energy.txt buy_back_energy -ascii
  end
 
% --- Executes on button press in plot_button.
function plot_button_Callback(hObject, eventdata, handles)
% hObject    handle to plot_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

guidata(hObject, handles);

  acc_money_saved = textread('C:\vars\acc_money_saved.txt');
  payback_period = textread('C:\vars\payback_period.txt');
  period_loan = textread('C:\vars\period_loan.txt'); 
  monthly_repay = textread('C:\vars\monthly_repay.txt'); 
  money_saved = textread('C:\vars\money_saved.txt'); 
  APR = textread('C:\vars\APR.txt'); 
  energy_produced_wind = textread('C:\vars\energy_produced_wind.txt'); 
  energy_produced_PV = textread('C:\vars\energy_produced_PV.txt');
  bat_energy = textread('C:\vars\bat_energy.txt');
  radiation = textread('C:\vars\radiation.txt');
  wind_speed = textread('C:\vars\wind_speed.txt');
  system_cost = textread('C:\vars\system_cost.txt');
  
 

guidata(hObject, handles);
popup_sel_index_graph_select_1 = get(handles.graph_select_1, 'Value');
 switch popup_sel_index_graph_select_1
     case 1
         axes(handles.axes1)
         
         for i = 0:2
             yearly_money_saved(i+1) = acc_money_saved((1+i*8768));
         end
         
         %This extrapolates the line to produce a graph
         for i = 4:(fix(payback_period))*2
             yearly_money_saved(i) = yearly_money_saved(i-1) + (yearly_money_saved(i-2) - yearly_money_saved(i-3));
         end
        %This line indicates the pay back time
         plot([1 (payback_period*2)],[monthly_repay*period_loan*12 monthly_repay*period_loan*12],'g')
         
         hold on;
         plot(yearly_money_saved)
         legend('Payback line',' Sum of money saved')
         hold off;
         grid on;
        
     case 2
         
        for i = 0:35 
        monthly_money_saved(i+1) = acc_money_saved((1+i*fix(730.66666)));
        end
        
        axes(handles.axes1);
        plot(monthly_money_saved);
        
        grid on;
        
        
     case 3
        if period_loan <= 0 || APR <= 0
            monthly_repay = 0;
        end
        for i = 1:36
        money_per_month = sum(money_saved(1 +((i-1)*730):1:(730*i)));
        discounted_repay(i) = monthly_repay - money_per_month;
        end
        axes(handles.axes1);
        plot([0 36],[monthly_repay monthly_repay],'r')
        hold on
        plot(discounted_repay);
        legend('Monthly repayment','Discounted monthly repayment')
        grid on;
        hold off;
        
       
     case 4
       plot(acc_money_saved)
        axes(handles.axes1)
     case 5
        plot(acc_money_saved)
        axes(handles.axes1)
 end 

guidata(hObject, handles);
popup_sel_index_graph_select_2 = get(handles.graph_select_2, 'Value');
 switch popup_sel_index_graph_select_2
     case 1
         axes(handles.axes2);
         plot(energy_produced_wind + energy_produced_PV)

     case 2
         axes(handles.axes2);
         plot(energy_produced_wind)
        
         

     case 3
        axes(handles.axes2);
         plot(energy_produced_PV)
     case 4
        axes(handles.axes2);
        plot(bat_energy)
        
     case 5
        axes(handles.axes2);
        plot(radiation)
        
     case 6
        axes(handles.axes2);
        plot(wind_speed)
 end  
% --- Executes during object creation, after setting all properties.
function Calculate_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Calculate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes during object creation, after setting all properties.
function user_rotor_diameter_CreateFcn(hObject, eventdata, handles)
% hObject    handle to user_rotor_diameter (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end













% --- Executes during object creation, after setting all properties.
function plot_button_CreateFcn(hObject, eventdata, handles)
% hObject    handle to plot_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called





function buy_back_price_Callback(hObject, eventdata, handles)
% hObject    handle to buy_back_price (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of buy_back_price as text
%        str2double(get(hObject,'String')) returns contents of buy_back_price as a double


% --- Executes during object creation, after setting all properties.
function buy_back_price_CreateFcn(hObject, eventdata, handles)
% hObject    handle to buy_back_price (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end














function grant_Callback(hObject, eventdata, handles)
% hObject    handle to grant (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of grant as text
%        str2double(get(hObject,'String')) returns contents of grant as a double


% --- Executes during object creation, after setting all properties.
function grant_CreateFcn(hObject, eventdata, handles)
% hObject    handle to grant (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end




% --- Executes on button press in wind_yes.
function wind_yes_Callback(hObject, eventdata, handles)
% hObject    handle to wind_yes (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of wind_yes


% --- Executes on button press in wind_no.
function wind_no_Callback(hObject, eventdata, handles)
% hObject    handle to wind_no (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of wind_no




% --- Executes on button press in solar_yes.
function solar_yes_Callback(hObject, eventdata, handles)
% hObject    handle to solar_yes (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of solar_yes




% --- Executes on button press in storage_yes.
function storage_yes_Callback(hObject, eventdata, handles)
% hObject    handle to storage_yes (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of storage_yes


% --- Executes on button press in loan_yes.
function loan_yes_Callback(hObject, eventdata, handles)
% hObject    handle to loan_yes (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of loan_yes


